<?php
namespace app\admin\service\agent;

use app\admin\model\agent\Withdrawal as Model;

class Withdrawal 
{
    private Model $model;

    public function __construct(){
        $this->model = new Model;
    }

    /**
     * 新增提现记录
     * 
     * @param int $agent_id 代理商ID
     * @param int $card_id 银行卡ID
     * @param float $price 提现金额
     * @return void
     * 
     */
    public function create(int $agent_id, int $card_id, float $price){
        $save = $this->model
        ->create([
            'agent_id'  =>  $agent_id,
            'card_id'  =>  $card_id,
            'price'  =>  $price,
            'status'    =>  0,
            'msg'   =>  ''
        ]);

    }

    /**
     * 提现申请通过
     * 这里后期可以将审核人的信息记录下来
     * 
     * @param int $admin_id 通过人的id
     * @param int $withdrawal_id 记录的id
     * @return void
     * @throws Exception 数据查询异常
     */
    public function confirm(int $admin_id, int $withdrawal_id){
        $row = $this->model
        ->where([
            'withdrawal_id' =>  $withdrawal_id
        ])
        ->find();
        if(!$row){
            throw new \Exception("数据查询失败");
        }
        $row->status = 1; // 已打款状态
        $row->save();

    }

    /**
     * 拒绝提现申请
     * 
     * @param int $admin_id 通过人的ID
     * @param int $withdrawal_id 提现表ID
     * @param string $msg 拒绝原因
     * 
     * \app\admin\facade\AgentWith::refuse(1,20, '1拒绝了你的打款');
     */
    public function refuse(int $admin_id, int $withdrawal_id,string $msg){
        $row = $this->model
        ->where([
            'withdrawal_id' =>  $withdrawal_id
        ])
        ->find();
        if(!$row){
            throw new \Exception("数据查询失败");
        }
        $row->status = 2; // 拒绝打款
        $row->msg = $msg;
        $row->save();
    }

}